
//
//  JXOrginal.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/3/2.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SnapKit

let CellMargin = 10

class JXOrginal: UIView {

    // 定义一个约束,用于记录底部
    var bottomConstarint: Constraint?
    
    var statusViewModel: JXStatusViewModel?{
        didSet{
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            
            contentLabel.text = statusViewModel?.statusModel?.text
            
            veriftyImageView.image = statusViewModel?.verifity_image
            
            levelImageView.image = statusViewModel?.level_image


//            //清空约束
            bottomConstarint?.uninstall()
//            
            if let pic_urls = statusViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
                
                print(statusViewModel?.statusModel?.text)
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //底部应该等于配图视图的底部
                    bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
              })
                
                //传值

                //定义一个临时数组进行数据拼接
                var result = [JXPictureViewModel]()
                for model in pic_urls{
                    
                    let viewModel  = JXPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                }
                
                pictureView.pictureList = result
            
            } else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //没有配图视图,底部应该在 contentLabel的底部
                    bottomConstarint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        

        }
        
    }
    
   override init(frame: CGRect) {
        super.init(frame: frame)
            setupUi()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    private func setupUi(){
        
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
//        头像约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
//        认证图片约束
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
//        微博名字约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
//        会员等级约束
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
//        发表时间约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
        }
//        发表来源约束
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
//        发表内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
            
            make.right.equalTo(self.snp_right).offset(-CellMargin)
            
//            make.bottom.equalTo(self.snp_bottom)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
        }
        

        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView.snp_bottom)
        }
        
    }
   
    // 懒加载
    //    原创微博 --
    //    头像
    private lazy var photoImageView: UIImageView = {
       let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
        return img
        
    }()
    
//    认证图片
    private lazy var veriftyImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_vip")
        return img
        
        }()
//    微博名字
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
       label.text = "微博名字"
        
        label.font = UIFont.systemFontOfSize(15)
        return label
        
        }()
//    会员等级
    private lazy var levelImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "common_icon_membership_level1")
        return img
        
        }()
//    发表时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.text = "刚刚"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.orangeColor()
        return label
        
        }()
//    发表来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.text = "这是一个牛逼的微博"
        label.font = UIFont.systemFontOfSize(13)
        return label
        
        }()

//    发表内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.text = "疯了"
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        return label
        
        }()
   private lazy var pictureView: JXPictureView = JXPictureView()

}
